
(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.prodtype.eventPopupViewController', eventViewController);
	eventViewController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', '$log' ];
	function eventViewController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, $log) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("eventPopupViewController");
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {			
			log.debug('ctrl.dialogsModule.params=' + ctrl.dialogsModule.params);
			for (var pname in ctrl.dialogsModule.params['selectedRows'][0]) {
				log.debug(pname + '=' + ctrl.dialogsModule.params['selectedRows'][0][pname]);
			}
			
			if (ctrl.dialogsModule.params['selectedRows'][0]) {
				ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
				ctrl.gridOptions.data = 'vm.searchCriteria.list';
				ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
				ctrl.searchCriteria.eventCode = ctrl.dialogsModule.params['selectedRows'][0]['eventCode'];
				ctrl.searchCategory();							
				
				return authService.view(ctrl.dialogsModule.params['selectedRows'][0]).then(function(response) {
					ctrl.event = response.data || {};
				}, angular.noop);
			}
		};
		
		ctrl.searchCategory = function() {
			ctrl.searchCriteria.list = [];
			authService.doccatList(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				//gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};
		
		
		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.searchCategory();
		}		
	}
})(angular);

